package com.liangsui.mss.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liangsui.mss.constants.Constants;
import com.liangsui.mss.domain.CheckItem;
import com.liangsui.mss.dto.CheckItemDto;
import com.liangsui.mss.mapper.CheckItemMapper;
import com.liangsui.mss.service.CheckItemService;
import com.liangsui.mss.vo.DataGridView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
* @author zhang
* @description 针对表【sys_check_item(检查费用表)】的数据库操作Service实现
* @createDate 2022-12-05 16:41:03
*/
@Service
public class CheckItemServiceImpl extends ServiceImpl<CheckItemMapper, CheckItem> implements CheckItemService{
    @Autowired
    private CheckItemMapper checkItemMapper;

    @Override
    public DataGridView listCheckItemPage(CheckItemDto checkItemDto) {
        Page<CheckItem> page = new Page<>(checkItemDto.getPageNum(), checkItemDto.getPageSize());
        QueryWrapper<CheckItem> qw = new QueryWrapper<>();
        qw.like(StringUtils.isNotBlank(checkItemDto.getCheckItemName()),
                CheckItem.COL_CHECK_ITEM_NAME, checkItemDto.getCheckItemName());
        qw.like(StringUtils.isNotBlank(checkItemDto.getKeywords()),
                CheckItem.COL_KEYWORDS, checkItemDto.getKeywords());
        qw.eq(StringUtils.isNotBlank(checkItemDto.getTypeId()),
                CheckItem.COL_TYPE_ID, checkItemDto.getTypeId());
        qw.eq(StringUtils.isNotBlank(checkItemDto.getStatus()),
                CheckItem.COL_STATUS, checkItemDto.getStatus());
        this.checkItemMapper.selectPage(page, qw);
        return new DataGridView(page.getTotal(), page.getRecords());
    }

    /**
     * 查询所有可用的检查项目
     *
     * @return
     */
    @Override
    public List<CheckItem> selectAllCheckItem() {
        QueryWrapper<CheckItem> qw = new QueryWrapper<>();
        qw.eq(CheckItem.COL_STATUS, Constants.STATUS_TRUE);
        this.checkItemMapper.selectList(qw);
        return this.checkItemMapper.selectList(qw);
    }

    @Override
    public CheckItem getCheckItemById(Long checkItemId) {
        return this.checkItemMapper.selectById(checkItemId);
    }

    @Override
    public int addCheckItem(CheckItemDto checkItemDto) {
        CheckItem checkItem = new CheckItem();
        BeanUtil.copyProperties(checkItemDto, checkItem);
        checkItem.setCreateTime(DateUtil.date());
        checkItem.setCreateBy(checkItemDto.getSimpleUser().getUserName());
        return this.checkItemMapper.insert(checkItem);
    }

    @Override
    public int updateCheckItem(CheckItemDto checkItemDto) {
        CheckItem checkItem = new CheckItem();
        BeanUtil.copyProperties(checkItemDto, checkItem);
        checkItem.setUpdateBy(checkItemDto.getSimpleUser().getUserName());
        return this.checkItemMapper.updateById(checkItem);
    }

    @Override
    public int deleteCheckItemByIds(Long[] checkItemIds) {
        List<Long> ids = Arrays.asList(checkItemIds);
        if (ids.size() > 0) {
            return this.checkItemMapper.deleteBatchIds(ids);
        }
        return 0;
    }
}




